package com.study.custom.mybatis;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import lombok.Data;

import javax.sql.DataSource;
import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

/**
 * @desc:
 * @author: panxiyi
 * @date: 2023/12/23 21:16
 */

@Data
public class Configuration {

    private String username;
    private String password;
    private String url;
    private String driver;

    //创建数据源
    private ComboPooledDataSource dataSource = new ComboPooledDataSource();

    private Map<String, Mapper> mappers = new HashMap<String, Mapper>();

    public void setMappers(Map<String, Mapper> mappers) {
        this.mappers.putAll(mappers);
    }

    private DataSource getDataSource(){
        //设置数据源配置
        try {
            dataSource.setUser(username);
            dataSource.setPassword(password);
            dataSource.setJdbcUrl(url);
            dataSource.setDriverClass(driver);
        } catch (PropertyVetoException e) {
            e.printStackTrace();
        }
        return dataSource;
    }

    public Connection getConnection(){
        try {
            return getDataSource().getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }



}
